package com.hmall.cart.listener;

import com.hmall.cart.service.ICartService;
import com.hmall.common.constant.MQConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * @author 勾新杰
 * @version 1.0
 * @date 2024/10/31 11:42
 */
@Component
@RequiredArgsConstructor
public class ClearCartListener {

    private final ICartService cartService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.CART_CLEAR_QUEUE_NAME, durable = "true"),
            exchange = @Exchange(name = MQConstants.CREATE_ORDER_EXCHANGE_NAME, type = "topic"),
            key = MQConstants.CREATE_ORDER_ROUTING_KEY
    ))
    public void clearCart(Collection<Long> itemIds, Message message){
        cartService.removeByItemIds(itemIds, message.getMessageProperties().getHeader(MQConstants.MESSAGE_HEADER_USER_INFO));
    }
}
